﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using System.Web.Services;
using Meqs.Utilities;

public partial class Credit_Teacher_RuleSettings_ReviewEval : System.Web.UI.Page
{
    int currHistoryId = 0, currCauseId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string s = Request.Params["historyId"];
            int.TryParse(s, out currHistoryId);

            s = Request.Params["causeId"];
            int.TryParse(s, out currCauseId);

            int type = 0;
            s = Request.Params["type"];
            int.TryParse(s, out type);

            BindHeadTable(type,currHistoryId, currCauseId);
        }
    }
    private int _tableWidth;

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "\\download\\" + DateTime.Now.Ticks.ToString() + "样表模板.xls";
        HttpContext.Current.Session["filepath"] = save_path;
        Meqs.Utilities.ExcelUtil.ExcelExport(save_path, "样表模板", 2, 0, cells);
        return "";
    }
    protected void BindHeadTable(int type,int historyId,int causeId)
    {
        int numCell = 4;
        _tableWidth = 0;

        TableRow row = new TableRow();
        TableRow subRow = new TableRow();
        row.Style["background-color"] = "#D4F1F9";
        subRow.Style["background-color"] = "#D4F1F9";

        TableCell cell1 = new TableCell();
        cell1.Text = "序号";
        cell1.RowSpan = 2;
        cell1.Style["width"] = "40px";
        row.Cells.Add(cell1);
        _tableWidth += 40;

        TableCell cell2 = new TableCell();
        cell2.Text = "姓名";
        cell2.RowSpan = 2;
        cell2.Style["width"] = "100px";
        row.Cells.Add(cell2);
        _tableWidth += 100;


        MadeDynamicCell(ref numCell,ref row,ref subRow,type);

        //int index = 0;
        //string SchoolMark = Common.GetConstSchoolMark();
        //int SchoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        //int Term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;

        //IStuCauseEvalTypeLogic mtLogic = Container<IStuCauseEvalTypeLogic>.Instance;
        //IStuCauseEvalItemLogic miLogic = Container<IStuCauseEvalItemLogic>.Instance;
        //IList<StuCauseEvalType> list;
        //if (SchoolYear == -1 || Term == -1)
        //{
        //    list = mtLogic.GetAllStuCauseEvalType(currCauseId, SchoolMark);
        //    //Hashtable table = new Hashtable();
        //    //table.Add("SchoolMark",Common.GetConstSchoolMark());
        //    //table.Add("HistoryID",historyId);
        //    //list = (IList<StuCauseEvalType>)mtLogic.FindAll(table);
        //}
        //else
        //{
        //    list = mtLogic.GetAllStuCauseEvalType(currCauseId, SchoolMark, SchoolYear, Term);
        //}
        //foreach (StuCauseEvalType mt in list)
        //{
        //    TableCell cell = new TableCell();
        //    cell.Text = mt.Caption;
        //    IList<StuCauseEvalItem> miList = miLogic.GetItemByType(SchoolMark, mt.RID);
        //    cell.ColumnSpan = miList.Count;
        //    cell.Style["width"] = (60 * miList.Count) + "px";
        //    _tableWidth += 60 * miList.Count;
        //    foreach (StuCauseEvalItem item in miList)
        //    {
        //        TableCell subCell = new TableCell();
        //        subCell.Text = item.Caption + (item.Type == 1 ? "(分)" : "(次)");
        //        subCell.Style["width"] = "60px";
        //        subRow.Cells.Add(subCell);
        //        ParamVal pv = new ParamVal();
        //        pv.Index = index;
        //        pv.ItemID = item.RID;
        //    //    _paramVals.Add(pv);
        //        index++;
        //        numCell++;
        //    }
        //    row.Cells.Add(cell);
        //}
        TableCell cell4 = new TableCell();
        cell4.Text = "小计";
        cell4.RowSpan = 2;
        cell4.Style["width"] = "100px";
        row.Cells.Add(cell4);
        _tableWidth += 100;

        TableCell cell3 = new TableCell();
        cell3.Text = "备注";
        cell3.RowSpan = 2;
        cell3.Style["width"] = "100px";
        row.Cells.Add(cell3);
        _tableWidth += 100;


        tblHead.Rows.Add(row);
        tblHead.Rows.Add(subRow);
        tblHead.Style["width"] = _tableWidth + "px";

        //初始化空行
        int indexNum = 1;
        for (int i = 0; i < 10; i++)
        {
            row = new TableRow();
            for (int j = 0; j < numCell; j++)
            {
                TableCell newCell = new TableCell();
                if (j == 0)
                    newCell.Text = indexNum.ToString();
                else
                    newCell.Text = "";
                row.Cells.Add(newCell);
            }
            indexNum++;
            tblHead.Rows.Add(row);
        //    tblHead.Rows.Add(subRow);
        //    tblHead.Style["width"] = _tableWidth + "px";
        }
    }

    private void MadeDynamicCell(ref int numCell,ref TableRow row,ref TableRow subRow,int type)
    { 
        int index = 0;
        string SchoolMark = Common.GetConstSchoolMark();
        int SchoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        int Term = SchoolCommon.Sch_SchoolInfo.SchoolTerm;

        if (type == 1)
        {
            //StuCauseHistory
            IStuCauseEvalTypeLogic mtLogic = Container<IStuCauseEvalTypeLogic>.Instance;
            IStuCauseEvalItemLogic miLogic = Container<IStuCauseEvalItemLogic>.Instance;
            IList<StuCauseEvalType> list;
            if (SchoolYear == -1 || Term == -1)
            {
                list = mtLogic.GetAllStuCauseEvalType(currCauseId, SchoolMark);
            }
            else
            {
                list = mtLogic.GetAllStuCauseEvalType(currCauseId, SchoolMark, SchoolYear, Term);
            }
            foreach (StuCauseEvalType mt in list)
            {
                TableCell cell = new TableCell();
                cell.Text = mt.Caption;
                IList<StuCauseEvalItem> miList = miLogic.GetItemByType(SchoolMark, mt.RID);
                cell.ColumnSpan = miList.Count;
                cell.Style["width"] = (60 * miList.Count) + "px";
                _tableWidth += 60 * miList.Count;
                foreach (StuCauseEvalItem item in miList)
                {
                    TableCell subCell = new TableCell();
                    subCell.Text = item.Caption + (item.Type == 1 ? String.Format("({0}分)", item.Param * item.Operator) : "(次)");
                    subCell.Style["width"] = "60px";
                    subRow.Cells.Add(subCell);
                    ParamVal pv = new ParamVal();
                    pv.Index = index;
                    pv.ItemID = item.RID;
                    //    _paramVals.Add(pv);
                    index++;
                    numCell++;
                }
                row.Cells.Add(cell);
            }
        }

        if (type == 2)
        { 
            //ClassHistory

            IClassEvalTypeLogic mtLogic = Container<IClassEvalTypeLogic>.Instance;
            IClassEvalItemLogic miLogic = Container<IClassEvalItemLogic>.Instance;
            IList<ClassEvalType> list;

            if (SchoolYear == -1 || Term == -1)
            {
                ISchoolInfoLogic _schoolLogic = Container<ISchoolInfoLogic>.Instance;
                SchoolInfo school = _schoolLogic.FindBySchoolMark(SchoolMark);
                SchoolYear = school.SchoolYear;
                Term = school.SchoolTerm;
            }
            list = mtLogic.GetAllClassEvalType(currHistoryId,SchoolMark, SchoolYear, Term);
            foreach (ClassEvalType mt in list)
            {
                TableCell cell = new TableCell();
                cell.Text = mt.Caption;
                IList<ClassEvalItem> miList = miLogic.GetItemByType(SchoolMark, mt.RID);
                cell.ColumnSpan = miList.Count;
                cell.Style["width"] = (60 * miList.Count) + "px";
                _tableWidth += 60 * miList.Count;
                foreach (ClassEvalItem item in miList)
                {
                    TableCell subCell = new TableCell();
                    subCell.Text = item.Caption + (item.Type == 1 ? String.Format("({0}分)", item.Param * item.Operator) : "(次)");
                    subCell.Style["width"] = "60px";
                    subRow.Cells.Add(subCell);
                    ParamVal pv = new ParamVal();
                    pv.Index = index;
                    pv.ItemID = item.RID;
                    //    _paramVals.Add(pv);
                    index++;
                    numCell++;
                }
                row.Cells.Add(cell);
            }
        }
    }
}
